package app.modules.cloudpivot.organization;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.authine.cloudpivot.engine.exceptions.DataSourceException;
import com.authine.cloudpivot.platform.organization.Department;
import com.authine.cloudpivot.platform.organization.application.DepartmentService;
import com.authine.cloudpivot.platform.organization.infrastructure.sync.customize.OrgSyncAdapterRuntimeService;
import com.authine.cloudpivot.platform.organization.infrastructure.sync.customize.dto.DepartmentDTO;
import com.authine.cloudpivot.platform.organization.infrastructure.sync.customize.dto.IncrementParseParams;
import com.authine.cloudpivot.platform.organization.infrastructure.sync.customize.dto.IncrementParseResult;
import com.authine.cloudpivot.platform.organization.infrastructure.sync.customize.dto.UserDTO;
import com.authine.lib.deprecated.util.SpringBeanUtils;

/**
 * 自定义组织同步实现类
 *
 * @author LiuGangQiang Create in 2024/04/12
 */
public class CustomOrgRuntimeServiceImpl implements OrgSyncAdapterRuntimeService {
    private static final Logger LOG = LoggerFactory.getLogger(CustomOrgRuntimeServiceImpl.class);

    private String corpId;
    private String requireParam;
    private String optionalParam;

    public CustomOrgRuntimeServiceImpl(String corpId, Map<String, String> configMap) {
        this.corpId = corpId;
        this.requireParam = configMap.get("require_param");
        this.optionalParam = configMap.get("optional_param");
    }

    @Override
    public List<DepartmentDTO> getAllChildDepartments(String departmentId) throws DataSourceException {
        LOG.info("===>>根部门ID:{}", departmentId);
        LOG.info("===>>必填参数:{}", requireParam);
        LOG.info("===>>可选参数:{}", optionalParam);
        /* 根部门 */
        DepartmentDTO root = new DepartmentDTO();
        root.setId(departmentId);

        /* 一级部门 */
        DepartmentDTO dto = new DepartmentDTO();
        String pid = UUID.randomUUID().toString().replace("-", "");
        dto.setId(pid);
        dto.setName("一级部门");
        dto.setParentId(departmentId);

        /* 二级部门 */
        DepartmentDTO dto1 = new DepartmentDTO();
        dto1.setId(UUID.randomUUID().toString().replace("-", ""));
        dto1.setName("二级部门一");
        dto1.setParentId(pid);

        DepartmentDTO dto2 = new DepartmentDTO();
        dto2.setId(UUID.randomUUID().toString().replace("-", ""));
        dto2.setName("二级部门二");
        dto2.setParentId(pid);

        return Arrays.asList(root, dto, dto1, dto2);
    }

    @Override
    public String getRootDepartmentId() throws DataSourceException {
        Department rootDept = SpringBeanUtils.applicationContext.getBean(DepartmentService.class).get(corpId);
        return rootDept.getId();
    }

    @Override
    public List<UserDTO> getUsersByDepartmentId(String arg0) throws DataSourceException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public IncrementParseResult parseIncBody(IncrementParseParams params) throws DataSourceException {
        // TODO Auto-generated method stub
        return OrgSyncAdapterRuntimeService.super.parseIncBody(params);
    }
    
}
